package com.dormitory_springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dormitory_springboot.bean.Homemaster;
import com.dormitory_springboot.bean.Student;
import com.dormitory_springboot.form.RuleForm;
import com.dormitory_springboot.form.StudentForm;
import com.dormitory_springboot.service.StudentService;
import com.dormitory_springboot.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @auther 夏杨
 **/
@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    StudentService studentService;
    @GetMapping("/login")
    public ResultVO login(RuleForm ruleForm){
        return studentService.login(ruleForm);
    }
    @PostMapping("/addStudents")
    public void addStudents(@RequestBody List<Student> students){
        students.forEach(student -> {
            studentService.save(student);
        });
    }
    @GetMapping("/getUsefulDorid")
    public List<String> getUsefulDorid(){
        List<Student> students = studentService.list();
        List<String> dorids = new ArrayList<>();
        students.forEach(student -> {
            if(!dorids.contains(student.getDorid())){
                dorids.add(student.getDorid());
            }
        });
        return dorids;
    }
    @GetMapping("/getUsefulDept")
    public List<String> getUsefulDept(){
        List<Student> students = studentService.list();
        List<String> depts = new ArrayList<>();
        students.forEach(student -> {
            if(!depts.contains(student.getDept())){
                depts.add(student.getDept());
            }
        });
        return depts;
    }
    @GetMapping("/getStudent/{sex}")
    public Page getStudent(StudentForm studentForm,@PathVariable("sex")String sex){
        List<Student> students = new ArrayList<>();
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        if(!studentForm.getSid().equals(""))
            queryWrapper.like("Sid",studentForm.getSid());
        if(!studentForm.getDept().equals(""))
            queryWrapper.eq("dept",studentForm.getDept());
        if(!studentForm.getDorid().equals(""))
            queryWrapper.eq("dorid",studentForm.getDorid());
        if(studentForm.isChecked())
            queryWrapper.eq("isHome",!studentForm.isChecked());
        queryWrapper.eq("sex",sex);
        Page<Student> page = new Page<>(studentForm.getPage(),studentForm.getPagesize());
        return studentService.page(page,queryWrapper);
    }
    @DeleteMapping ("/deleteStudents/{sex}")
    public void deleteStudents(StudentForm studentForm,@PathVariable("sex")String sex){
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        if(!studentForm.getSid().equals(""))
            queryWrapper.like("Sid",studentForm.getSid());
        if(!studentForm.getDept().equals(""))
            queryWrapper.eq("dept",studentForm.getDept());
        if(!studentForm.getDorid().equals(""))
            queryWrapper.eq("dorid",studentForm.getDorid());
        if(studentForm.isChecked())
            queryWrapper.eq("isHome",!studentForm.isChecked());
        queryWrapper.eq("sex",sex);
        studentService.remove(queryWrapper);
    }
    @GetMapping("/getStudentById/{sid}")
    public Student getStudentById(@PathVariable("sid") String sid){
        return studentService.getById(sid);
    }
    @GetMapping("/editStudent/{sid}")
    public void editStudent(Student student,@PathVariable("sid") String sid){
        UpdateWrapper<Student> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("sid",sid);
        studentService.update(student,updateWrapper);
        updateWrapper.set("sid",student.getSid());
        studentService.update(updateWrapper);
    }
    @GetMapping("/deleteOneStu/{sid}")
    public void deleteOneStu(@PathVariable("sid") String sid){
        studentService.removeById(sid);
    }
    @GetMapping("/getEchartsMWdata")
    public List<Map<String,Object>> getEchartsMWdata(){
        QueryWrapper queryWrapper1 = new QueryWrapper();
        QueryWrapper queryWrapper2 = new QueryWrapper();
        queryWrapper1.eq("sex","男");
        Long man = (long) studentService.count(queryWrapper1);
        queryWrapper2.eq("sex","女");
        Long woman = (long) studentService.count(queryWrapper2);
        Map<String,Object> mapM = new HashMap<>();
        mapM.put("value",man);
        mapM.put("name","男");
        Map<String,Object> mapW = new HashMap<>();
        mapW.put("value",woman);
        mapW.put("name","女");
        List<Map<String,Object>> list = new ArrayList<>();
        list.add(mapM);
        list.add(mapW);
        return list;
    }
    @GetMapping("/getEchartsINdata/{sex}")
    public List<Map<String,Object>> getEchartsINdata(@PathVariable("sex")String sex){
        QueryWrapper queryWrapper1 = new QueryWrapper();
        QueryWrapper queryWrapper2 = new QueryWrapper();
        queryWrapper1.eq("sex",sex);
        queryWrapper2.eq("sex",sex);
        queryWrapper1.eq("isHome",true);
        Long isHome = (long) studentService.count(queryWrapper1);
        queryWrapper2.eq("isHome",false);
        Long notHome = (long) studentService.count(queryWrapper2);
        Map<String,Object> mapM = new HashMap<>();
        mapM.put("value",isHome);
        mapM.put("name","到寝");
        Map<String,Object> mapW = new HashMap<>();
        mapW.put("value",notHome);
        mapW.put("name","未到寝");
        List<Map<String,Object>> list = new ArrayList<>();
        list.add(mapM);
        list.add(mapW);
        return list;
    }
    @GetMapping("/changePassById/{password}/{sid}")
    public void changePassById(@PathVariable("sid")String sid,@PathVariable("password") String password){
        UpdateWrapper<Student> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("sid",sid).set("password",password);
        studentService.update(updateWrapper);
    }
    @GetMapping("isHome/{sid}")
    public boolean isHome(@PathVariable("sid")String sid){
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sid",sid);
        Student student = studentService.getOne(queryWrapper);
        if(student == null)
            return false;
        else
            return student.isIshome();
    }
    @GetMapping("/changeIsHomeById/{ishome}/{sid}")
    public void changePassById(@PathVariable("ishome")Boolean ishome,@PathVariable("sid")String sid){
        UpdateWrapper<Student> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("sid",sid).set("ishome",ishome);
        studentService.update(updateWrapper);
    }
    @GetMapping("/getNumOfSameStu")
    public int getNumOfSameStu(@RequestParam("sid")String sid){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.likeRight("sid",sid);
        return studentService.count(queryWrapper);
    }
    @GetMapping("/getStudent2")
    public List<Student> getStudent2(@RequestParam("sex")String sex){
        QueryWrapper queryWrapper =new QueryWrapper();
        queryWrapper.eq("sex",sex);
        return studentService.list(queryWrapper);
    }
    @GetMapping("/GetStudentByPhone")
    public Student getStudentByPhone(@RequestParam("phone")String phone,@RequestParam(value = "sid",defaultValue = "")String sid,@RequestParam(value = "password",defaultValue = "")String password){
        QueryWrapper queryWrapper =new QueryWrapper();
        if(!"".equals(phone))
            queryWrapper.eq("phone",phone);
        if(!"".equals(sid))
            queryWrapper.eq("sid",sid);
        if(!"".equals(password))
            queryWrapper.eq("password",password);
        return studentService.getOne(queryWrapper);
    }
}
